home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / General / Script Tools / Examples / Regular Expression Example II < prev    next >
Text File  |  1993-07-08  |  3KB  |  100 lines

  1. --
  2. --    Get the user to locate the params.test file in the Examples directory
  3. --
  4. set paramFile to choose file with prompt "Select params.good or params.bad" of type "TEXT"
  5. set refNum to open file paramFile for reading
  6.  
  7. --
  8. --    Compile the pattern strings we're going to look for
  9. --
  10. set comment to compile regular expression ".*#.*"
  11. set param1 to compile regular expression "Default Width:[     ]*([0-9]*)"
  12. set param2 to compile regular expression "Default Height:[     ]*([0-9]*)"
  13. set param3 to compile regular expression "Username:[     ]*([A-Za-z0-9]*)"
  14. set param4 to compile regular expression "Username:[     ]*\"(.*)\""
  15.  
  16. --
  17. --    Populate parameter variables with default values
  18. --
  19. set defWidth to 0
  20. set defHeight to 0
  21. set defUsername to "unknown"
  22.  
  23. --
  24. --    Read through the file looking for parameters
  25. --
  26. set paramMatched to true
  27. try
  28.     repeat while paramMatched
  29.         
  30.         --
  31.         --    Read in the next line
  32.         --
  33.         set input to read file refNum
  34.         
  35.         --
  36.         --    If the line is not a comment line or a blank line then check to see
  37.         --    which parameter is being set
  38.         --
  39.         if not matched of (match regular expression comment to input) and length of input ≠ 0 then
  40.             set paramMatched to false
  41.             
  42.             --
  43.             --    Default width?
  44.             --
  45.             set result to match regular expression param1 to input
  46.             if (matched of result) then
  47.                 set defWidth to match 1 of result
  48.                 set paramMatched to true
  49.             end if
  50.             
  51.             --
  52.             --    Default height?
  53.             --
  54.             set result to match regular expression param2 to input
  55.             if (matched of result) then
  56.                 set paramMatched to true
  57.                 set defHeight to match 1 of result
  58.             end if
  59.             
  60.             --
  61.             --    First username form?
  62.             --
  63.             set result to match regular expression param3 to input
  64.             if (matched of result) then
  65.                 set paramMatched to true
  66.                 set defUsername to match 1 of result
  67.             end if
  68.             
  69.             --
  70.             --    Second username form?
  71.             --
  72.             set result to match regular expression param4 to input
  73.             if (matched of result) then
  74.                 set paramMatched to true
  75.                 set defUsername to match 1 of result
  76.             end if
  77.         end if
  78.     end repeat
  79.     
  80.     --
  81.     --    If we get here it means the repear look existed because of a bad parameter.  Otherwise
  82.     --    the error trap woould have cought the end of file.
  83.     --
  84.     display dialog "Bad parameter file line: “" & input & "”" ¬
  85.         buttons {"Cancel"} ¬
  86.         default button "Cancel"
  87.     close file refNum
  88. on error
  89.     --
  90.     --    Assume EOF
  91.     --
  92.     close file refNum
  93. end try
  94.  
  95. --
  96. --    Show the parameters as they are now
  97. --
  98. if paramMatched then ¬
  99.     display dialog "Def Width: " & defWidth & ", Def. Height: " & defHeight & ", Username: “" & defUsername & "”" ¬
  100.         buttons {"OK"} default button "OK"